package com.newbingo.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.googlecode.jsonplugin.annotations.JSON;
import com.newbingo.dto.AgentDto;
import com.newbingo.entity.Agent;
import com.newbingo.service.imple.IAgentService;
import com.newbingo.service.imple.IBettingItemService;
import com.newbingo.service.imple.IMemberService;
import com.newbingo.util.AgentTree;
import com.newbingo.util.Pager;
import com.opensymphony.xwork2.ActionSupport;

/*
 * 代理商报表
 */
public class AgentReportAction extends ActionSupport {
	private IBettingItemService bettingItemService;
	private IAgentService agentService;
	private IMemberService memberService;
	
	//根据代理商的ID和日期的代号查询出日期段内代理商的报表
	private Integer agentId;
	private Integer dateNum=1; //初始化值，默认查询为一周的报表
	private List data=new ArrayList();
	
	//分页查询出会员报表
	private int start;
	private int limit=10;
	private Pager pager;
	
	//根据登录用户ID生成代理商和下级代理的树型节点
	private String strTree;
	
	//根据登录的ID查询出子代理的，生成树形菜单
	public String getAgentTree(){
		HttpSession session=ServletActionContext.getRequest().getSession();
		Object obj=session.getAttribute("user");
		if(obj!=null){
			if(obj instanceof AgentDto){
				AgentDto agentDto=(AgentDto)obj;
				Agent agent=new Agent();
				System.out.println("代理为"+agentDto.getAgentType());
				//如果登录名为股东，则查询出代理商
				if(agentDto.getAgentType().equals(1)){
					//获取登录股东的实体类
					Agent subAgent=agentService.findById(agentDto.getUno());
					agent=subAgent.getParentAgent();
					System.out.println(agent.getTrueName()+"\t"+agent.getUname());
				}else{
					agent=agentService.findById(agentDto.getUno());
				}
				strTree=AgentTree.getAgentTree(agent).toString();
			}
		}
		System.out.println(strTree);
		return "agentTree";
	}
	
	/*
	 * 根据点击选择代理商节点查询出此代理商的游戏桌报表
	 */
	public String findAgentReoprt(){
		data=bettingItemService.findAgentReportByTime(agentId, dateNum);
		return SUCCESS;
	}
	
	/*
	 * 根据代理商获取代理商下会员的投注报表
	 */
	public String findMemberReport(){
//		pager=bettingItemService.getMemberReportByAgent(agentId, dateNum, start, limit);
		pager=memberService.findMemberReportByPager(start, limit, agentId, dateNum);
		return "memberReport";
	}
	
	public Integer getAgentId() {
		return agentId;
	}
	public void setAgentId(Integer agentId) {
		this.agentId = agentId;
	}
	public Integer getDateNum() {
		return dateNum;
	}
	public void setDateNum(Integer dateNum) {
		this.dateNum = dateNum;
	}

	public List getData() {
		return data;
	}

	public void setData(List data) {
		this.data = data;
	}

	@JSON(serialize=false)
	public IBettingItemService getBettingItemService() {
		return bettingItemService;
	}

	public void setBettingItemService(IBettingItemService bettingItemService) {
		this.bettingItemService = bettingItemService;
	}

	public String getStrTree() {
		return strTree;
	}

	public void setStrTree(String strTree) {
		this.strTree = strTree;
	}
	@JSON(serialize=false)
	public IAgentService getAgentService() {
		return agentService;
	}

	public void setAgentService(IAgentService agentService) {
		this.agentService = agentService;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public IMemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(IMemberService memberService) {
		this.memberService = memberService;
	}

}
